package com.defan.api.utils;

import java.sql.*;

/**
 * @func 用于连接Oracle数据库
 * Created by 张俊强~ on 2017/10/19.
 */
public class OracleJdbcUtils {
    private static String url = "jdbc:oracle:thin:@fmwdbdev:1521:fmwdbdev";
    private static String user = "df_stage";
    private static String pwd = "dfstage";

    private OracleJdbcUtils() {

    }

    // 1、注册驱动oracle.jdbc.driver.OracleDriver
    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("数据库驱动加载失败！");
        }
    }

    // 2、建立一个连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, pwd);
    }

    // 5、关闭资源
    public static void free(ResultSet rs, Statement stmt, Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null)
                        conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}